package com.taijihuabao.modules.controller;

import com.github.pagehelper.PageInfo;
import com.taijihuabao.common.exception.ServiceException;
import com.taijihuabao.common.jsonresult.ApplicationResponseCode;
import com.taijihuabao.common.jsonresult.JsonResult;
import com.taijihuabao.common.redisconfig.UserLoad;
import com.taijihuabao.modules.entity.DeptUserEntity;
import com.taijihuabao.modules.entity.UserEntity;
import com.taijihuabao.modules.enums.optenums.dept.DeptEnum;
import com.taijihuabao.modules.model.dept.user.DeptUserDTO;
import com.taijihuabao.modules.model.dept.user.DeptUserPageDTO;
import com.taijihuabao.modules.service.DeptUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 部门用户管理
 * @author Xiao
 */
@Api(tags = "部门用户管理")
@RestController
@RequestMapping("/api/v1/dept/user")
public class DeptUserController {

    @Autowired
    private DeptUserService deptUserService;
    /**
     * 获取部门用户列表(带分页)
     *
     * @param deptUserDTO
     * @return
     */
    @ApiOperation("获取部门用户列表(带分页)")
    @GetMapping
    public JsonResult selectAllDept(DeptUserPageDTO deptUserDTO) {
        PageInfo pageInfo = deptUserService.selectAllDept(deptUserDTO);
        return JsonResult.success(pageInfo);
    }

    /**
     * 获取所有部门信息
     *
     * @return
     */
    @ApiOperation("获取所有部门用户信息")
    @GetMapping("/seletor")
    public JsonResult selectDepts() {
        List<DeptUserEntity> data = deptUserService.selectDepts();
        return JsonResult.success(data);
    }

    /**
     * 通过部门用户Id获取信息
     *
     * @param deptUserDTO
     * @return
     */
    @ApiOperation("通过部门用户Id获取信息")
    @GetMapping("/id")
    public JsonResult selectDeptById(DeptUserPageDTO deptUserDTO) {
        DeptUserEntity data = deptUserService.selectDeptById(deptUserDTO.getId());
        return JsonResult.success(data);
    }

    /**
     * 通过用户Id获取信息
     *
     * @param deptUserDTO
     * @return
     */
    @ApiOperation("通过用户Id获取信息")
    @GetMapping("/userId")
    public JsonResult selectDeptByUserId(DeptUserPageDTO deptUserDTO) {
        List<DeptUserEntity> data = deptUserService.selectDeptByUserId(deptUserDTO.getUserId());
        return JsonResult.success(data);
    }

    /**
     * 通过部门编码获取信息
     *
     * @param deptUserDTO
     * @return
     */
    @ApiOperation("通过部门编码获取信息")
    @GetMapping("/deptCode")
    public JsonResult selectDeptByDeptCode(DeptUserPageDTO deptUserDTO) {
        List<DeptUserEntity> data = deptUserService.selectDeptByDeptCode(deptUserDTO.getDeptCode());
        return JsonResult.success(data);
    }

    /**
     * 通过用户名称获取信息
     *
     * @param deptUserDTO
     * @return
     */
    @ApiOperation("通过用户名称获取信息")
    @GetMapping("/name")
    public JsonResult selectDeptByName(DeptUserPageDTO deptUserDTO) {
        List<DeptUserEntity> data = deptUserService.selectDeptByName(deptUserDTO.getName());
        return JsonResult.success(data);
    }

    /**
     * 通过部门角色编码获取信息
     *
     * @param deptUserDTO
     * @return
     */
    @ApiOperation("通过部门角色编码获取信息")
    @GetMapping("/deptRole")
    public JsonResult selectDeptByDeptRole(DeptUserPageDTO deptUserDTO) {
        List<DeptUserEntity> data = deptUserService.selectDeptByDeptRole(deptUserDTO.getDeptRole());
        return JsonResult.success(data);
    }

    /**
     * 新增部门用户信息
     *
     * @param deptUserEntity
     * @return
     */
    @ApiOperation("新增部门用户信息")
    @PostMapping
    public JsonResult insertDept(@RequestBody DeptUserDTO deptUserEntity) {
        //判断部门角色 超级管理员和本部门的管理员才能修改 普通职员不行
        UserEntity loginUser = UserLoad.getUser();
        if(1>deptUserJurisdiction(loginUser)){
            throw new ServiceException(ApplicationResponseCode.DEPT_OUT_BOUNDS);
        }
        deptUserService.insertDept(deptUserEntity);
        return JsonResult.success(deptUserEntity);
    }

    /**
     * 部门用户信息编辑
     *
     * @param deptUserEntity
     * @return
     */
    @ApiOperation("部门用户信息编辑")
    @PutMapping
    public JsonResult updateDept(@RequestBody DeptUserDTO deptUserEntity) {
        //判断部门角色 超级管理员和本部门的管理员才能修改 普通职员不行
        UserEntity loginUser = UserLoad.getUser();
        if(1>deptUserJurisdiction(loginUser)){
            throw new ServiceException(ApplicationResponseCode.DEPT_OUT_BOUNDS);
        }
        deptUserService.updateDept(deptUserEntity);
        return JsonResult.success(deptUserEntity);
    }

    /**
     * 删除部门用户信息
     *
     * @param deptUserEntity
     * @return
     */
    @ApiOperation("删除部门用户信息")
    @DeleteMapping
    public JsonResult deleteDept(@RequestBody DeptUserDTO deptUserEntity) {
        //判断部门角色 超级管理员和本部门的管理员才能修改 普通职员不行
        UserEntity loginUser = UserLoad.getUser();
        if(1>deptUserJurisdiction(loginUser)){
            throw new ServiceException(ApplicationResponseCode.DEPT_OUT_BOUNDS);
        }
        deptUserService.deleteDept(deptUserEntity);
        return JsonResult.success(deptUserEntity.getId());
    }

    /**
     * 判断部门角色 超级管理员和本部门的管理员才能修改 普通职员不行
     * @param loginUser
     * @return
     */
    private int deptUserJurisdiction(UserEntity loginUser){
        List<DeptUserEntity> list = deptUserService.selectDeptByUserId(loginUser.getId());
        DeptUserEntity d1 = new DeptUserEntity();
        for(DeptUserEntity d2 : list){
                //判断是否为总经理办公室
                if(DeptEnum.GENERALMANAGER.getCode().equals(d2.getDeptCode())){
                    d1 = d2;
                    break;
                }
                //判断是否为本部门的管理员
                DeptUserEntity dd = deptUserService.selectDeptById(d2.getId());
                if(d2.getDeptCode().equals(dd.getDeptCode())){
                    if(d2.getDeptRole().equals(dd.getDeptRole())){
                        d1 = d2;
                        break;
                    }
                }
            }
        if(Objects.isNull(d1.getUserId())){
            throw new ServiceException(ApplicationResponseCode.DEPT_OUT_BOUNDS);
        }
        return 1;
    }
}
